package com.miao.teacher.ui;

import com.miao.teacher.presenters.OnMenuClickedListener;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class BaseFragment extends Fragment {

	OnMenuClickedListener mCallback;
	/*
	 * (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
	 */
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try {
			mCallback = (OnMenuClickedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnMenuClickedListener");
		}
	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(getActivity());
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(getActivity());
	}
	/*
	 * 判断是否需要显示侧拉菜单
	 */
	private void OnMenuClick() {
		mCallback.onMenuButtonClicked();
	}
	/*
	 * 绑定侧拉菜单显示事件
	 */
	protected void setMenuClickView(View view)
	{
		view.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				OnMenuClick();
			}
			
		});
	}
	// 通知子类 接收到UI更新消息
	protected void RecMsgFromHandler(Message msg) {

	}

	// UI消息 更新UI
	protected Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {// 此方法在ui线程运行
			RecMsgFromHandler(msg);
		}
	};
	/*
	 * 获取屏幕宽度 px
	 */
	protected int getXPx() {
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		dm = getActivity().getApplicationContext().getResources().getDisplayMetrics();
		return dm.widthPixels;
	}

	/*
	 * 获取屏幕高度 px
	 */
	protected int getYPx() {
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		dm = getActivity().getApplicationContext().getResources().getDisplayMetrics();
		return dm.heightPixels;
	}
	/*
	 * Toast显示消息
	 */
	protected void ToastShowMsg(String msg) {
		Toast.makeText(getActivity(), msg, 3000).show();
	}

	protected void ToastShowMsg(int resId) {
		Toast
				.makeText(getActivity(), getResources().getString(resId),
						3000).show();
	}
	public int dip2px(float dipValue) {
		final float scale = getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}
}
